/*
 * Copyright (C) 2010 TopCoder Inc., All Rights Reserved.
 */
package com.hercules.android.receiver.painter;

import android.graphics.Canvas;
import android.net.Uri;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.VideoView;
import android.widget.RelativeLayout.LayoutParams;

import com.hercules.android.receiver.Receiver;
import com.hercules.android.receiver.ReceiverUtil;
import com.hercules.android.receiver.entities.XREVideo;
import com.hercules.android.receiver.entities.XREView;

/**
 * <p>
 * This class is a painter class which will draw a XREVideo resource on a canvas.
 * </p>
 * 
 * @author TCSASSEMBER
 * @version 1.0
 */
public class XREVideoPainter extends XREPainter {
    /**
     * Constructor with a <code>Receiver</code> arguments.
     * 
     * @param receiver
     *            the <code>Receiver</code> instance.
     * @throws IllegalArgumentException
     *             if receiver if null
     */
    public XREVideoPainter(Receiver receiver) {
        super(receiver);
    }

    /**
     * This method will paint the resource on canvas.
     * 
     * @param view
     *            the resource to paint
     * @param canvas
     *            the canvas that the resource painted on
     * @throws IllegalArgumentException
     *             if arguments is null
     * @throws XREPainterException
     *             if any error occurs
     */
    public void paint(XREView view, Canvas canvas) throws XREPainterException {
        // check arguments
        ReceiverUtil.checkNull("view", view);
        ReceiverUtil.checkNull("canvas", canvas);

        if (view.getResource() == 0) {
            return;
        }
        if (!view.getVisible() || !view.getPainting()) {
            return;
        }

        XREVideo targetVideo = (XREVideo) receiver.getApplicationData().getElementsXREIdToElement().get(
                view.getResource());

        // if the video have already been rendered to screen, then just return
        if (targetVideo == null || targetVideo.getVideoView() != null) {
            return;
        }
        if (ReceiverUtil.isEmpty(targetVideo.getUrl())) {
            throw new XREPainterException("Video's url can't be empty.");
        }

        // get the transform matrix to determine the position of the video view
        float[] values = new float[9];
        canvas.getMatrix().getValues(values);

        // create a video view
        VideoView videoView = new VideoView(receiver.getAppRootView().getContext());
        MediaController mediaController = new MediaController(receiver.getAppRootView().getContext());
        Uri video = Uri.parse(targetVideo.getUrl());
        videoView.setMediaController(mediaController);
        videoView.setVideoURI(video);
        targetVideo.setVideoView(videoView);

        // put the video view to the root layout (RelativeLayout)
        LayoutParams lp = new LayoutParams(view.getDimensions()[0], view.getDimensions()[1]);
        lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        lp.setMargins((int) values[2], (int) values[5], 0, 0);
        receiver.getAppRootView().addView(videoView, lp);

        // add the new created video view to the global video list (so we can clear the resource later)
        receiver.getApplicationData().getVideos().add(videoView);
        if (targetVideo.getAutoPlay()) {
            // play the video
            videoView.start();
        }
    }
}
